﻿using Hwh_Shop.Models.Database;
using Hwh_Shop.Models;
using Hwh_Shop.Models.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace Hwh_Shop.Controllers
{
	/// <summary>
	/// 购物车模块
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ShoppingCarController : ControllerBase
	{
		// 定义数据库上下文
		private readonly Hwh_ShopContext _db;

		/// <summary>
		/// 构造方法 构造注入
		/// 把数据库上下文初始化
		/// </summary>
		/// <param name="db"></param>
		public ShoppingCarController(Hwh_ShopContext db)
		{
			_db = db;
		}

		/// <summary>
		/// 加入购物车
		/// </summary>
		/// <param name="comNo">商品编号</param>
		/// <returns></returns>
		[HttpGet]
		[Authorize]//标签 接口需要授权
		public string AddShoppingCar(int comNo)
		{
			// 获取商品信息
			var commodity = _db.Commodities.Find(comNo);
			if (commodity == null)
			{
				return "商品不存在";
			}
			//获取用户信息           登录用户 消息载荷 查询第一条（条件是Type==）   获取登录用户的用户名
			var userTel = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; // 获取登录用户的手机号
																												 // 查询购物车是否已经存在该商品
			var repeat = _db.ShoppingCars.Count(x => x.ComNo == comNo && x.ShopCarUser == userTel);
			if (repeat > 0)
			{
				// 购物车已存在该商品 数量+1
				var shoppingCar = _db.ShoppingCars.FirstOrDefault(x => x.ComNo == comNo && x.ShopCarUser == userTel);
				shoppingCar.ShopCarCount++;
			}
			else
			{
				// 购物车添加该商品 数量为1
				_db.ShoppingCars.Add(new ShoppingCar
				{
					ComNo = comNo,
					ShopCarCount = 1,
					ShopCarUser = userTel
				});
			}
			var row = _db.SaveChanges();
			if (row > 0)
			{
				return "添加成功";
			}
			else
			{
				return "添加失败";
			}
		}

		/// <summary>
		/// 减少购物车
		/// </summary>
		/// <param name="comNo">商品编号</param>
		/// <returns></returns>
		[HttpGet]
		[Authorize]//标签 接口需要授权
		public string ReductionShoppingCar(int comNo)
		{
			// 获取商品信息
			var commodity = _db.Commodities.Find(comNo);
			if (commodity == null)
			{
				return "商品不存在";
			}
			//获取用户信息           登录用户 消息载荷 查询第一条（条件是Type==）   获取登录用户的用户名
			var userTel = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; // 获取登录用户的手机号
																												 // 查询购物车是否已经存在该商品
			var repeat = _db.ShoppingCars.Count(x => x.ComNo == comNo && x.ShopCarUser == userTel);
			// 购物车已存在该商品 数量-1
			if (repeat > 0)
			{
				var shoppingCar = _db.ShoppingCars.FirstOrDefault(x => x.ComNo == comNo && x.ShopCarUser == userTel);
				// 数量大于1 减少数量
				if (shoppingCar.ShopCarCount > 1)
				{
					shoppingCar.ShopCarCount--;
				}
				else // 数量等于1 删除该商品
				{
					_db.ShoppingCars.Remove(shoppingCar);
				}
			}
			else
			{
				return "购物车不存在该商品";
			}
			var row = _db.SaveChanges();
			if (row > 0)
			{
				return "减少成功";
			}
			else
			{
				return "减少失败";
			}
		}

		/// <summary>
		/// 删除购物车商品
		/// </summary>
		/// <param name="comNo">商品编号</param>
		/// <returns></returns>
		[HttpGet]
		[Authorize]//标签 接口需要授权
		public string DeleteShoppingCar(int comNo)
		{
			// 获取商品信息
			var commodity = _db.Commodities.Find(comNo);
			if (commodity == null)
			{
				return "商品不存在";
			}
			//获取用户信息           登录用户 消息载荷 查询第一条（条件是Type==）   获取登录用户的用户名
			var userTel = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; // 获取登录用户的手机号
																												 // 查询购物车是否已经存在该商品
			var repeat = _db.ShoppingCars.Count(x => x.ComNo == comNo && x.ShopCarUser == userTel);
			// 购物车已存在该商品 删除该商品
			if (repeat > 0)
			{
				var shoppingCar = _db.ShoppingCars.FirstOrDefault(x => x.ComNo == comNo && x.ShopCarUser == userTel);
				_db.ShoppingCars.Remove(shoppingCar);
			}
			else
			{
				return "购物车不存在该商品";
			}
			var row = _db.SaveChanges();
			if (row > 0)
			{
				return "删除成功";
			}
			else
			{
				return "删除失败";
			}
		}

		/// <summary>
		/// 查询购物车商品
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		[Authorize]//标签 接口需要授权
		public List<ShoppingCar_GetShoppingCar> GetShoppingCar()
		{
			//获取用户信息           登录用户 消息载荷 查询第一条（条件是Type==）   获取登录用户的用户名
			var userTel = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value; // 获取登录用户的手机号
																												 // 查询用户的购物车
			var shoppingCar = _db.ShoppingCars.Where(x => x.ShopCarUser == userTel);
			// 购物车为空 返回null
			if (shoppingCar.Count() == 0)
			{
				return null;
			}
			// 新建一个列表，用于存储购物车商品信息，并返回
			List<ShoppingCar_GetShoppingCar> list = new List<ShoppingCar_GetShoppingCar>();
			foreach (var item in shoppingCar.ToList())
			{
				// 查询商品信息
				var commodity = _db.Commodities.Find(item.ComNo);
				list.Add(new ShoppingCar_GetShoppingCar
				{
					ComName = commodity.ComName,
					ComPhoto = commodity.ComPhoto,
					ComNo = commodity.ComNo,
					ShopCarCount = item.ShopCarCount.ToString(),
					ShopCarTotalPrice = (int)(item.ShopCarCount * commodity.ComPrice)
				});
			}
			return list;
		}
	}
}
